package com.jt.sys.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jt.common.vo.JsonResult;
import com.jt.common.vo.PageObject;
import com.jt.common.vo.SysUserDeptResult;
import com.jt.sys.dao.SysUserRoleDao;
import com.jt.sys.entity.SysUser;
import com.jt.sys.service.SysUserService;

@Controller
@RequestMapping("/user/")
public class SysUserController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	@RequestMapping("doUserListUI")
	public String doUserListUI(){
		return "sys/user_list";
	}
	@RequiresPermissions("sys:user:add")
	@RequestMapping("doUserSaveEditUI")
	public String doUserSaveEditUI(){
		return "sys/user_edit";
	}
	@RequiresPermissions("sys:user:update")
	@RequestMapping("doUserUpdateEditUI")
	public String doUserUpdateEditUI(){
		return "sys/user_edit";
	}
	@RequestMapping("doFindPageObjects")
	@ResponseBody
	@RequiresPermissions("sys:user:view")
	public JsonResult doFindPageObjects(String username,Integer pageCurrent){
		PageObject<SysUserDeptResult> pageObject = sysUserService.findPageObjects(username, pageCurrent);
		return new JsonResult(pageObject);
	}
	@RequestMapping("doValidById")
	@ResponseBody
	@RequiresPermissions("sys:user:valid")
	public JsonResult doValidById(Integer id,Byte valid){
		sysUserService.validById(id, valid);
		return new JsonResult("Change OK");
	}
	@RequestMapping("doFindRoles")
	@ResponseBody
	public JsonResult doFindRoles(){
		return new JsonResult(
				sysUserService.findRoles());
	}
	@RequestMapping("doSaveObject")
	@ResponseBody
	public JsonResult doSaveObject(SysUser entity,Integer[] roleIds){
		sysUserService.saveObject(entity, roleIds);
		return new JsonResult("save ok");
	}
	@RequestMapping("doUpdateObject")
	@ResponseBody
	public JsonResult doUpdateObject(SysUser entity,Integer[] roleIds){
		sysUserService.updateObject(entity, roleIds);
		return new JsonResult("update ok");
	}
	@RequestMapping("doFindRoleIdsByUserId")
	@ResponseBody
	private Map<String , Object> doFindRoleIdsByUserId(Integer userId) {
		List<Integer> list = sysUserRoleDao.findRoleIdsByUserId(userId);
		Map<String, Object> map = new HashMap<>();
		map.put("roleId", list);
		return map;
	}
	@RequestMapping("doFindPermissionByUsername")
	@ResponseBody
	public JsonResult doFindPermissionByUsername(String username){
		List<String> list = sysUserService.findPermissionsByUsername(username);
		return new JsonResult(list);
	}
}
